home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998…tember: Reference Library / Dev.CD Sep 98 RL2.toast / What's New / Software Development Kits / MacOS USB DDK / Examples / PrinterClassDriver / usbprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-20  |  6.5 KB  |  277 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        usbprint.c
  3.  
  4.     Contains:    usb printer class device communication
  5.                     (installed in UnitTable)
  6.  
  7. */
  8. #include "PrinterClassDriver.h"
  9.  
  10. #ifndef __DEVICES__
  11. #include <devices.h>
  12. #endif
  13.  
  14. #ifndef __FILES__
  15. #include <files.h>
  16. #endif
  17.  
  18. #define kMaskLowByte    0x0FF
  19.  
  20. extern pascal OSErr    DRVRDone( OSErr err, DCtlPtr ctl, IOParamPtr pb );
  21.  
  22. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23.     Name:        GetPrinterStruct
  24.  
  25.     Input Parameters:    IOParamPtr        i/o parameter block
  26.         
  27.     Output Parameters:
  28.         usbPrinterPBStruct    * pointer to the device
  29.         
  30.     Description:
  31.         Given a device control block, map it to a usb device's data structure
  32.  
  33.     Change History:
  34.         28 Feb 1998,    oja:        Original version.
  35. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  36. static struct usbPrinterPBStruct    *
  37. GetPrinterStruct( DCtlPtr ctl )
  38. {
  39.     return (struct usbPrinterPBStruct *) ctl->dCtlStorage;
  40. }
  41.  
  42. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43.     Name:        Read
  44.  
  45.     Input Parameters:
  46.         
  47.     Output Parameters:
  48.         <none>
  49.         
  50.     Description:
  51.         
  52.     Change History:
  53.         11 Jun 1998,    oja:        return pb->ioResult, not usbprint->in.usbStatus
  54.          4 Apr 1998,    oja:        Fix handshaking: do not set pb->ioResult
  55.         28 Feb 1998,    oja:        Original version.
  56. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  57. static OSErr
  58. Read (IOParamPtr pb, DCtlPtr ctl)
  59. {
  60.     struct usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  61.  
  62.     //
  63.     //    if we have a unidirectional interface
  64.     //        report a read error
  65.     //        (only status supported by unidirectional is Centronics compatible)
  66.     //
  67.     pb->ioResult = paramErr;    // assume bad
  68.  
  69.     if ( usbprint->interface->interfaceProtocol == kUSBPrintClassProtocolUnidirectional )
  70.         pb->ioResult = readErr;
  71.     else if ( usbprint != NULL )
  72.         (*usbprint->qread)( pb, ctl, usbprint );    //    map the read param block into the usb param block
  73.  
  74.     return pb->ioResult;
  75.  
  76. }
  77.  
  78. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  79.     Name:        Write
  80.  
  81.     Input Parameters:
  82.         
  83.     Output Parameters:
  84.         <none>
  85.         
  86.     Description:
  87.         
  88.     Change History:
  89.         11 Jun 1998,    oja:        return pb->ioResult, not usbprint->out.usbStatus
  90.          4 Apr 1998,    oja:        Fix handshaking: do not set pb->ioResult
  91.         28 Feb 1998,    oja:        Original version.
  92. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  93. static OSErr
  94. Write (IOParamPtr pb, DCtlPtr ctl)
  95. {
  96.     struct usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  97.  
  98.     pb->ioResult = paramErr;    // assume bad
  99.  
  100.     if ( usbprint != NULL )
  101.         (*usbprint->qwrite)( pb, ctl, usbprint );    //    map the write param block into the usb param block
  102.  
  103.     return pb->ioResult;
  104. }
  105.  
  106. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  107.     Name:        DRVROpen
  108.  
  109.     Input Parameters:
  110.         
  111.     Output Parameters:
  112.         <none>
  113.         
  114.     Description:
  115.         
  116.     Change History:
  117.         28 Feb 1998,    oja:        Original version.
  118. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  119.  
  120. pascal OSErr
  121. DRVROpen(CntrlParam *pb, DCtlPtr dce)
  122. {
  123. #if 0
  124.     dce->dCtlFlags |= dCtlEnableMask | dStatEnableMask | dWritEnableMask |
  125.                                 dReadEnableMask | dNeedLockMask | dRAMBased;
  126. #endif
  127.  
  128.     return noErr;
  129. }
  130.  
  131. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132.     Name:        DRVRClose
  133.  
  134.     Input Parameters:
  135.         
  136.     Output Parameters:
  137.         <none>
  138.         
  139.     Description:
  140.         
  141.     Change History:
  142.         28 Feb 1998,    oja:        Original version.
  143. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  144.  
  145. pascal OSErr
  146. DRVRClose(CntrlParam *pb, DCtlPtr ctl)
  147. {
  148.     return noErr;
  149. }
  150.  
  151. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  152.     Name:        DRVRStatus
  153.  
  154.     Input Parameters:
  155.         
  156.     Output Parameters:
  157.         <none>
  158.         
  159.     Description:
  160.         
  161.     Change History:
  162.         30 Jun 1998,    oja:        pass through messages for kDrvrCentronicsStatus,
  163.                                         kDrvr1284IdString,kDrvrSoftReset
  164.         28 Feb 1998,    oja:        Original version.
  165. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  166.  
  167. pascal OSErr
  168. DRVRStatus(CntrlParam *pb, DCtlPtr ctl)
  169. {
  170.     OSErr                err;
  171.     struct             usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  172.  
  173.     switch ( pb->csCode )
  174.     {
  175.     case kDrvrCentronicsStatus:    //  USB device: centronics status
  176.     case kDrvr1284IdString:     //  USB device: 1284 capability string
  177.     case kDrvrSoftReset:    //  USB device: soft reset
  178.         if ( usbprint != NULL )
  179.             (*usbprint->qstatus)( pb, ctl, usbprint );    //    map the status param block into the usb param block
  180.         err = pb->ioResult;
  181.         break;
  182.     case kDrvrNumDevices:    
  183.         err =  noErr;
  184.         break;
  185.     case 1:    
  186.     case 2:    // deprecated
  187.     default:
  188.         err = paramErr;
  189.         break;
  190.     }
  191.     return err;
  192. }
  193.  
  194. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  195.     Name:        DRVRControl
  196.  
  197.     Input Parameters:
  198.         csCode                        csParam
  199.         ------                        -------
  200.         kDrvrPrivateSetStorage    pointer to the USB device class storage
  201.  
  202.     Output Parameters:
  203.         <none>
  204.         
  205.     Description:
  206.         
  207.     Change History:
  208.         12 Jun 1998,    oja:        default to noErr (was uninitialized with killCode)
  209.         11 Jun 1998,    oja:        abort active usb transactions
  210.          4 Apr 1998,    oja        Removed unused code for private done i/o
  211.                                          pass error code to DrvrDone
  212.         28 Feb 1998,    oja:        Original version.
  213. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  214.  
  215. pascal OSErr
  216. DRVRControl(CntrlParam *pb, DCtlPtr ctl)
  217. {
  218.     struct usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  219.     OSErr                err = noErr;
  220.  
  221.     switch ( pb->csCode )
  222.     {
  223.     case killCode:
  224.         if ( usbprint != NULL )
  225.             (*usbprint->qabort)( pb->ioCRefNum, usbprint );    //    cancel outstanding transactions
  226.         break;
  227.     case kDrvrPrivateSetStorage:
  228.         //
  229.         //    reference the class driver's private storage
  230.         //        it's not a handle, but devices.h thinks it should be
  231.         //
  232.         ctl->dCtlStorage = (Handle) *((Ptr *) &pb->csParam[0] );
  233.         break;
  234.     default:
  235.         err = paramErr;
  236.         break;
  237.     }
  238.     return err;
  239. }
  240.  
  241. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  242.     Name:        DRVRPrime
  243.  
  244.     Input Parameters:
  245.         
  246.     Output Parameters:
  247.         <none>
  248.         
  249.     Description:
  250.         
  251.     Change History:
  252.         28 Feb 1998,    oja:        Original version.
  253. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  254.  
  255. pascal OSErr
  256. DRVRPrime(CntrlParam *pb, DCtlPtr ctl)
  257. {
  258.     OSErr    err = paramErr;
  259.     //
  260.     //    switch on the low order byte to dispatch reads and writes
  261.     //
  262.     if ( (pb->ioTrap & kMaskLowByte) == aRdCmd )
  263.         err = Read( (IOParamPtr) pb, ctl );
  264.     else if ( (pb->ioTrap & kMaskLowByte) == aWrCmd )
  265.         err = Write( (IOParamPtr) pb, ctl );
  266.  
  267.     //
  268.     //    get the ioResult in case the completion routine has already executed
  269.     //    
  270.     if ( err == noErr )
  271.         err = pb->ioResult;
  272.  
  273.     return err;
  274. }
  275.  
  276. // eof
  277.